/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xos_http.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../helper_objs/head.h"
#include "../data_struct/head.h"
#include "../helper_xos/head.h"
#include "../config/head.h"
#include "../macro/head.h"
#include "../impl/head.h"
#include "../msg/head.h"
#include "msg_main.h"

namespace xos_http
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    msg_main::msg_main()
    {
    }

    msg_main::~msg_main()
    {
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    // 
    // proc
    // 
    int msg_main::proc( xos_box::i_msg*& pMsg )
    {
        int ret = 1;

        int nMsg = pMsg->get_msg();

        switch( nMsg )
        {
            // startting
            // 
            case MAIN_STARTING:
            {
                startting();
            }
            break;
            case MAIN_START_FAILED:
            {
                start_failed();
            }
            break;
            case MAIN_STARTED:
            {
                startted();
            }
            break;

            // 
            // running
            // 
            case MAIN_HEART:
            {
                heart();
            }
            break;

            // 
            // stopping
            // 
            case MAIN_STOPPING:
            {
                stopping();
            }
            break;
            case MAIN_STOPPED:
            {
                stopped();
            }
            break;

            // 
            // other
            // 
            default:
            {
                ret = 0;
            }
            break;
        }

        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_main::startting()
    {
        int ret = 0;
        state::set_starting();
        helper_caller_callback::notify( net::NET_STARTTING );
        {
            helper_xos obj;
            obj.load();
        }
        helper_cat::start();
        return ret;
    }

    int msg_main::start_failed()
    {
        int ret = 0;
        state::set_stopped();
        helper_caller_callback::notify( net::NET_START_FAILED );
        return ret;
    }

    int msg_main::startted()
    {
        int ret = 0;
        state::set_running();
        helper_caller_callback::notify( net::NET_RUNNING );
        return ret;
    }

    int msg_main::heart()
    {
        int ret = 0;
        {
            helper_xos obj;
            obj.heart();
        }
        return ret;
    }

    int msg_main::stopping()
    {
        int ret = 0;
        LOG4( "stopping..." );
        state::set_stopping();
        helper_caller_callback::notify( net::NET_STOPPING );
        helper_main::get()->notify( MAIN_STOPPED );
        return ret;
    }

    int msg_main::stopped()
    {
        int ret = 0;
        state::set_stopped();
        helper_caller_callback::notify( net::NET_STOPPED );
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

} // xos_http
